package demo;

import java.sql.*;
import java.util.Scanner;

//根据学号和密码登录,登录可以查看自己的成绩了
public class Login {
    public static void main(String[] args) {
        //类内部把自己注册了一下
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return;
        }
        //连接字符串
        String url = "jdbc:mysql://localhost:3306/java68?useSSL=false&useUnicode=true&characterEncoding=UTF-8";
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        Scanner in=new Scanner(System.in);
        System.out.println("请输入学号:");
        long stuno=in.nextLong();
        in.nextLine();
        System.out.println("再输入密码:");
        //Sql注入漏洞
        //1:替换sql中的关键字 select,drop,where ,and or not, -- ,单引号:

        String pass=in.nextLine();//123' or 1=1 --
        //pass=pass.replace('\'','');工作量大!

        String sql="select * from s_student where stuno="+stuno+" and pass='"+pass+"'";
        System.out.println(sql);
        try {
            con = DriverManager.getConnection(url, "root", "root1234");
            st = con.createStatement();
            rs = st.executeQuery(sql);
            if(rs.next()){
                System.out.println("输入正确!");
                String sname=rs.getString("sname");
                java.util.Date enterDate=rs.getDate("enterdate");
                System.out.println("这个学生叫:"+sname+",入学时间是:"+enterDate);
            }else{
                System.out.println("输入错误,错误的学号或者密码!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                if (rs != null)
                    rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (st != null)
                    st.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (con != null)
                    con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


    }
}
